package com.hunlian.makelove.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.tools.DeviceTools;

import java.io.File;

/**
 * 
 * @author chenxian 程序中用到的配置信息
 */
public class AppConfig {
	/**
	 * 缓存等主目录格式
	 */
	public final static String HLW_HOME_PATH = File.separator + "sdcard"
			+ File.separator + "hlw" + File.separator;

	public final static int NET_TYPE_UNKNOWN = -1;
	public final static String NET_TYPE_UNKNOWN_NAME = "无网络";

	private static String userToken = "";
	//随机token
	public static String temporaryToken = "";
	public static long loginTime = 0;
	/**网络时间*/
	public static String netTime = "";
	public static String userName = "";
	public static String source = "";
	public static String openId = "";
	/**屏幕宽度*/
	public static int SCREEN_WIDTH;
	/**屏幕高度*/
	public static int SCREEN_HEIGHT;

	
	public String userId = "";  //登录用户的userId, 跟userToken匹配
	public static String authority = "";//用户权限
	public static int eduState = -1;//eduState,其值有4种状态，分别是0、1、2、3，0表示没有上传学历，1表示学历审核通过，2表示学历正在审核，3表示需审核没通过

	private SharedPreferences mSharedPreferences;
	private static AppConfig appConfig;
	
	public AppConfig() {
		mSharedPreferences = AppApplication.getAppContext().getSharedPreferences("hunlian", Context.MODE_PRIVATE);
	}
	
	public static synchronized AppConfig getInstance() {
		if (AppConfig.appConfig == null) {
			AppConfig.appConfig = new AppConfig();
		}

		return AppConfig.appConfig;
	}
	
	public static void setUserToken(String token) {
		loginTime = DeviceTools.getStringDayDatess();
		userToken = token;
	}


	// 保存登录用户信息
	public synchronized void saveLoginedUserExtendInfo(String tel, String userId) {
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString("tel", tel);
		editor.putString("userId", userId);
		editor.commit();
	}
	
	public synchronized void clearLoginedUserInfo() {
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString("tel", "");
		editor.putString("userId", "");
		editor.commit();
	}
	
	public synchronized void clearLoginedUserId() {
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString("userId", "");
		editor.commit();
	}
	
	public String getLoginedUserId() {
		return mSharedPreferences.getString("userId", "");
	}

	public synchronized void clearCompleted(){
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString("completed", "");
		editor.commit();
	}

	public void setCompleted(){
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString("completed", getLoginedUserId()+"-completed");
		editor.commit();
	}

	public String getCompleted(){
		return mSharedPreferences.getString("completed", "");
	}
	
	public String getLoginedTel() {
		return mSharedPreferences.getString("tel", "");
	}
	
	public void setLoginedTel(String tel) {
		mSharedPreferences.edit().putString("tel", tel).commit();
	}
	
	public synchronized void setStringValue(String key, String value) {
		mSharedPreferences.edit().putString(key, value).commit();
	}

	public synchronized void setBooleanValue(String key, boolean value) {
		mSharedPreferences.edit().putBoolean(key, value).commit();
	}
	
	public boolean getBooleanValue(String key, boolean defValue) {
		return mSharedPreferences.getBoolean(key, defValue);
	}
	
	public String getStringValue(String key, String defValue){
		return mSharedPreferences.getString(key, defValue);
	}
}
